# -*- coding:UTF-8 -*-
import numpy as np
import pylab as pl
import csv
import scipy as sp  
import pandas as pd
from scipy.optimize import leastsq


m = 4 


def real_func(x):
    return np.sin(2*np.pi*x) 
 
def fake_func(p, x):
    f = np.poly1d(p) 
    return f(x)

def residuals(p, y, x):
    return y - fake_func(p, x)


f = file('C:\\Users\\chengk\\Desktop\\MAIN2222.csv','rb')
reader = csv.reader(f)

artist_id = "aassdd"
data = np.zeros(183)
x = np.linspace(0,183,183);

i = 1

x2 = np.linspace(0,244,244)

x3 = np.zeros(53)
y3 = np.zeros(53)

j = 0


for line in reader:
    if artist_id != line[0]:
        j = 0
        p0 = np.random.randn(m)
        plsq = leastsq(residuals, p0, args=(data, x))
        pl.figure()
        pl.xlim(0,250)
        pl.plot(x,data,"r-")
        pl.plot(x3,y3,'bo')
        pl.plot(x2, fake_func(plsq[0], x2), label='fitted curve')
        pl.savefig('D:/bg/'+artist_id+'999.jpg')  
        data = np.zeros(183)
        artist_id = line[0]
    date = line[1]
    int_data = 0;
    if date[5] =='3':
        int_data=0
    elif date[5]=='4':
        int_data=31
    elif date[5] =='5':
        int_data=61
    elif date[5] == '6':
        int_data=92
    elif date[5] == '7':
        int_data=122
    elif date[5] == '8':
        int_data=153
    int_data+=(int(date[6])*10+int(date[7]))
    data[int_data - 1]=int(line[2])
    if int_data %7==0 or int_data %7 ==6:
        y3[j] = int(line[2])
        x3[j] = int_data
        j = j+1

        
